package socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;

public class ServerSocket {

	public static void main(String[] args) throws IOException {
		// 开启serverSocket
		ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
		// 这个channel监听一个端口
		serverSocketChannel.socket().bind(new InetSocketAddress(9999));
		// ServerSocketChannel可以设置成非阻塞模式。在非阻塞模式下，accept()
		// 方法会立刻返回，如果还没有新进来的连接,返回的将是null。 因此，需要检查返回的SocketChannel是否是null.
		serverSocketChannel.configureBlocking(false);
		while (true) {
			SocketChannel socketChannel = serverSocketChannel.accept();
			if (socketChannel != null) {
				// do someThing
				ByteBuffer buff=ByteBuffer.allocate(48);
				socketChannel.read(buff);
				buff.flip();
				System.out.println(buff);
			}
			// serverSocketChannel.close();
		}
	}
	
	public static void utf_8(){
		Charset charset = StandardCharsets.UTF_8; 
		
	}

}
